package com.dmgis.qxfw.dataservice.businessservice.yyhb.controller;

import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.airpollution;
import com.dmgis.qxfw.dataservice.businessservice.common.pojo.AllCityInfo;
import com.dmgis.qxfw.dataservice.businessservice.common.util.DateTime;
import com.dmgis.qxfw.dataservice.businessservice.yyhb.service.EnvirMonitorServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/xthb")
public class EnvirMonitorController {

	@Autowired
	private EnvirMonitorServiceI eMServiceImpl;


	/**
	 * 查询全县实况最新时间
	 * @return
	 */
	@RequestMapping("/allCityInitTime")
	@ResponseBody
	public String allCityInitTime(){
		Date date = eMServiceImpl.allCityInitTime();
		DateTime dt = new DateTime(date);
		return dt.toDateString();
	}


	/**
	 * 查询各站实况最新时间
	 * @return
	 */
	@RequestMapping("/everyCityInitTime")
	@ResponseBody
	public Object everyCityInitTime() {
		Date date = eMServiceImpl.everyCityInitTime();
		DateTime dt = new DateTime(date);
		ArrayList<String> result = new ArrayList<String>();
		result.add(dt.AddDays(-1).toDateTimeString());
		result.add(dt.toDateTimeString());
		return result;
	}

	/**
	 * 全县实况查询
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/allCityInfo")
	@ResponseBody
	public String allCityInfo(HttpServletRequest request) throws Exception{
		String start = request.getParameter("startTime");
		String end = request.getParameter("endTime");
		String startTime = start+" 00:00:00";
		String endTime = end+" 23:59:59";
		String check = "";
		if(request.getParameter("judge").equals("true")){
			check = "1";
		} else {
			check = "0";
		}
		String radio = request.getParameter("hourOrDay");
		String type = request.getParameter("type");
		String temp="";
		String tableDate="";
		String xValue="[";
		String y1Value="[";
		String y2Value="[";
		String y3Value="[";
		String y4Value="[";
		String y5Value="[";
		String y6Value="[";
		String y7Value="[";
		String y8Value="[";
		//列表数据
		String listData = "[";
		List<AllCityInfo> cityList = new ArrayList<AllCityInfo>();
		if("1".equals(check)){
			//查时间段数据
			if("时".equals(radio)){
				//按时，查airpollution
				cityList = eMServiceImpl.allCityInfo(startTime,endTime,"AirPollution");
			}else{
				//按日，查大气日数据汇总表
				cityList = eMServiceImpl.allCityInfo(startTime,endTime,"大气日数据汇总表");
			}
		}else if(!"1".equals(check)) {
			//查一天的数据
			if("时".equals(radio)){
				//按时，查airpollution
				cityList = eMServiceImpl.allCityInfo1(startTime,"AirPollution");
			}else{
				//按日，查大气日数据汇总表
				cityList = eMServiceImpl.allCityInfo1(startTime,"大气日数据汇总表");
			}
		}
		if(cityList.size()>0&&cityList!=null){
			for(AllCityInfo city : cityList){
				DateTime dt = new DateTime(city.getObservTime());
				if("时".equals(radio)){
					xValue += "'"+dt.toDateString()+ " " +dt.GetHour() + "',";
				}else{
					xValue += "'" +dt.toDateString() + "',";
				}
				if(city.getFractPM2p5()==null){
					y1Value += 0 + ",";
				}else{
					y1Value += city.getFractPM2p5() + ",";
				}
				if(city.getFractPM10()==null){
					y2Value += 0 + ",";
				}else{
					y2Value += city.getFractPM10() + ",";
				}
				if(city.getFractO38H()==null){
					y3Value += 0 + ",";
				}else{
					y3Value += city.getFractO38H() + ",";
				}
				if(city.getFractCO()==null){
					y4Value += 0 + ",";
				}else{
					y4Value += city.getFractCO() + ",";
				}
				if(city.getFractNO2()==null){
					y5Value += 0 + ",";
				}else{
					y5Value += city.getFractNO2()+ ",";
				}
				if(city.getFractSO2()==null){
					y6Value += 0 + ",";
				}else{
					y6Value += city.getFractSO2() + ",";
				}
				if(city.getFractO3()==null){
					y7Value += 0 + ",";
				}else{
					y7Value += city.getFractO3() + ",";
				}
				if(city.getAQI()==null){
					y8Value += 0 + ",";
				}else{
					y8Value += city.getAQI() + ",";
				}
				//列表数据
    			listData += "{\"dateChar\":\"" + dt.toDateString()
				+ "\",\"AQI\":\"" + city.getAQI()
				+ "\",\"FirstPollution\":\"" + city.getFirstPollution()
				+ "\",\"PollutionLevel\":\"" + city.getPollutionLevel()
				+ "\",\"ConcenPM2p5\":\"" + city.getConcenPM2p5()
				+ "\",\"FractPM2p5\":\"" + city.getFractPM2p5()
				+ "\",\"ConcenPM10\":\"" + city.getConcenPM10()
				+ "\",\"FractPM10\":\"" + city.getFractPM10()
				+ "\",\"ConcenCO\":\"" + city.getConcenCO()
				+ "\",\"FractCO\":\"" + city.getFractCO()
				+ "\",\"ConcenO3\":\"" + city.getConcenO3()
				+ "\",\"FractO3\":\"" + city.getFractO3()
				+ "\",\"ConcenO3_8H\":\"" + city.getConcenO38H()
				+ "\",\"FractO3_8H\":\"" + city.getFractO38H()
				+ "\",\"ConcenSO2\":\"" + city.getConcenSO2()
				+ "\",\"FractSO2\":\"" + city.getFractSO2()
				+ "\",\"ConcenNO2\":\"" + city.getConcenNO2()
				+ "\",\"FractNO2\":\"" + city.getFractNO2()
				+ "\"},";
			}
		}
		if (xValue.endsWith(",")) {
			xValue = xValue.substring(0, xValue.length() - 1);
		}
		xValue = xValue + "]";
		if (y1Value.endsWith(",")) {
			y1Value = y1Value.substring(0, y1Value.length() - 1);
		}
		y1Value = y1Value + "]";
		if (y2Value.endsWith(",")) {
			y2Value = y2Value.substring(0, y2Value.length() - 1);
		}
		y2Value = y2Value + "]";
		if (y3Value.endsWith(",")) {
			y3Value = y3Value.substring(0, y3Value.length() - 1);
		}
		y3Value = y3Value + "]";
		if (y4Value.endsWith(",")) {
			y4Value = y4Value.substring(0, y4Value.length() - 1);
		}
		y4Value = y4Value + "]";
		if (y5Value.endsWith(",")) {
			y5Value = y5Value.substring(0, y5Value.length() - 1);
		}
		y5Value = y5Value + "]";
		if (y6Value.endsWith(",")) {
			y6Value = y6Value.substring(0, y6Value.length() - 1);
		}
		y6Value = y6Value + "]";
		if (y7Value.endsWith(",")) {
			y7Value = y7Value.substring(0, y7Value.length() - 1);
		}
		y7Value = y7Value + "]";
		if (y8Value.endsWith(",")) {
			y8Value = y8Value.substring(0, y8Value.length() - 1);
		}
		y8Value = y8Value + "]";
		if(listData.endsWith(",")){
			listData = listData.substring(0, listData.length()-1);
		}
		listData = listData + "]";
		
		y1Value = "[{name:'PM2.5',data:" + y1Value + "},";
		y2Value = "{name:'PM10',data:" + y2Value + "},";
		y3Value = "{name:'O38H',data:" + y3Value + "},";
		y4Value = "{name:'CO',data:" + y4Value + "},";
		y5Value = "{name:'NO2',data:" + y5Value + "},";
		y6Value = "{name:'SO2',data:" + y6Value + "},";
		y7Value = "{name:'O3',data:" + y7Value + "},";
		y8Value = "{name:'AQI',data:" + y8Value + "}]";
		tableDate += "[{\"dateChar\":\"" + start + "\"}]";
		String title = "['"+start +"日全市空气质量实况" +"']";
		if("1".equals(check)){
			if(DateTime.parseDate(start).before(DateTime.parseDate(end))){
				title = "['"+start+"日至"+end +"日全市空气质量实况" +"']";
			}
		}
		temp = tableDate + "|" + xValue + "|" + y1Value + 
				y2Value + y3Value + y4Value + y5Value 
				+ y6Value + y7Value + y8Value+"|"+title+"|"+listData+"|['"+type.trim()+"']";
		return temp;
	}


	/**
	 * 各站实况查询
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/everyStationInfo")
	@ResponseBody
	public String everyStationInfo(HttpServletRequest request) throws Exception{
		String currElemVal = "";
		String start = request.getParameter("startTime");
		String check = "";
		if(request.getParameter("judge").equals("true")){
			check = "1";
		} else {
			check = "0";
		}
		String end = request.getParameter("endTime");
		String element = request.getParameter("element");
		String hourOrDay = request.getParameter("hourOrDay");
		String startTime = start+":00:00";
		String endTime = end+":59:59";
		String temp="";
		String tableDate="";
		String xValue="[";
		
		
		List<AllCityInfo> list = new ArrayList<AllCityInfo>();
		if("1".equals(check)){
			//查时间段数据
			if("时".equals(hourOrDay)){
				//按时，查airpollution
				list = eMServiceImpl.getEveryStationInfo(startTime,endTime,"AirPollution");
			}else{
				//按日，查大气日数据汇总表
				list = eMServiceImpl.getEveryStationInfo(startTime,endTime,"大气日数据汇总表");
			}
		}else if(!"1".equals(check)) {
			//查一天的数据
			if("时".equals(hourOrDay)){
				//按时，查airpollution
				list = eMServiceImpl.getEveryStationInfo1(startTime,"AirPollution");
			}else{
				//按日，查大气日数据汇总表
				list = eMServiceImpl.getEveryStationInfo1(startTime,"大气日数据汇总表");
			}
		}
		
		String element1 = "";
			switch(element){
			case "AQI" : 
				element1 = "AQI";
				break;
			case "PM2.5":
				element1 = "FractPM2p5";
				break;
			case "PM10" :
				element1 = "FractPM10";
				break;
			case "CO" :
				element1 = "FractCO";
				break;
			case "O3" :
				element1 = "FractO3";
				break;
			case "O3_8H" :
				element1 = "FractO38H";
				break;
			case "SO2" : 
				element1 = "FractSO2";
				break;
			case "NO2" :
				element1 = "FractNO2";
				break;
			default : break;
			}
//		String result = spellResult(element1, AllCityInfo.class, list,hourOrDay);
		String result = "";
		//tableDate += "[{\"dateChar\":\"" + start + "\"}]";
		tableDate += "[\"" + start + "\"]";
		String start1=start.substring(0,10);
		String title = "['"+start1 +"日各站空气质量实况" +"']";
		if("1".equals(check)){
			if (DateTime.parseDate(start).before(DateTime.parseDate(end))) {
				if ("时".equals(hourOrDay)) {
					title = "['" + start + "时至" + end + "时各站空气质量实况" + "']";
				} else {
					title = "['" + start.substring(0, 10) + "日至" + end.substring(0, 10) + "日各站空气质量实况" + "']";
				}
			}
		}

		// 拼接AQI最新数据，用于首页地图显示
		String aqiString = "[";
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date dateLast = formatter.parse((check == "0" ? start : end) + ":00:00");
		for (AllCityInfo ci : list) {
			if (dateLast.compareTo(ci.getObservTime()) == 0) {
				//Integer currElemVal = 0;
			    currElemVal = "";
				switch (element) {
					// case "AQI":
					// 	currElemVal = ci.getAQI();
					// 	break;
					// case "PM2.5":
					// 	currElemVal = ci.getFractPM2p5();
					// 	break;
					// case "PM10":
					// 	currElemVal = ci.getFractPM10();
					// 	break;
					// case "CO":
					// 	currElemVal = ci.getFractCO();
					// 	break;
					// case "O3":
					// 	currElemVal = ci.getFractO3();
					// 	break;
					// case "O3_8H":
					// 	currElemVal = ci.getFractO38H();
					// 	break;
					// case "SO2":
					// 	currElemVal = ci.getFractSO2();
					// 	break;
					// case "NO2":
					// 	currElemVal = ci.getFractNO2();
					// 	break;
					// default:
					// 	break;
					case "AQI":
						currElemVal = "[{name:'AQI',data:[" + ci.getAQI() + "]}]";
						break;
					case "PM2.5":
						currElemVal = "[{name:'PM2.5',data:[" + ci.getFractPM2p5() + "]}]";
						break;
					case "PM10":
						currElemVal = "[{name:'PM10',data:[" + ci.getFractPM10() + "]}]";
						break;
					case "CO":
						currElemVal = "[{name:'CO',data:[" + ci.getFractCO() + "]}]";
						break;
					case "O3":
						currElemVal = "[{name:'O3',data:[" + ci.getFractO3() + "]}]";
						break;
					case "O3_8H":
						currElemVal = "[{name:'O38H',data:[" + ci.getFractO38H() + "]}]";
						break;
					case "SO2":
						currElemVal = "[{name:'SO2',data:[" + ci.getFractSO2() + "]}]";
						break;
					case "NO2":
						currElemVal = "[{name:'NO2',data:[" + ci.getFractNO2() + "]}]";
						break;
				}
				aqiString += String.format(
						"{\"stationName\": \"%s\",\"lon\": \"%s\",\"lat\":\"%s\",\"elem\":\"%s\",\"level\":\"%s\",\"date\":\"%s\",\"firstpollution\":\"%s\"},",
						ci.getStationName(), ci.getLongitude(), ci.getLatitude(), currElemVal, ci.getPollutionLevel(),
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(ci.getObservTime()),ci.getFirstPollution() );
			}
		}
		if (aqiString.endsWith(",")) {
			aqiString = aqiString.substring(0, aqiString.length() - 1);
		}
		aqiString = aqiString + "]";

		temp = title +   result + "|" + tableDate + "|" + aqiString + "|" +currElemVal;
		return temp;
	}
	
	public String spellResult(String ele,Class<AllCityInfo> allcity,List<AllCityInfo> aList,String radio) throws Exception{
		String xValue="[";
		String xValue1="";
		String y1Value="[";
		String y2Value = "[";
		String listData = "[";
		Field[] fields = allcity.getDeclaredFields();
		for(Field field : fields){
			if(field.getName().contentEquals(ele)){
				for(AllCityInfo a1 : aList){
					if("湘潭一中".equals(a1.getStationName().trim())){
						if(field.get(a1)!=null){
							y1Value += field.get(a1)+ ",";
						}else{
							y1Value += 0+ ",";
						}
						DateTime dt = new DateTime(a1.getObservTime());
						if("时".equals(radio)){
							xValue += "'"+dt.toDateString()+ " " +dt.GetHour() + "',";
						}else{
							xValue += "'" +dt.toDateString() + "',";
						}

						//列表数据
						listData += "{\""+ xValue +"\":\"" + xValue
								+ "\"},";
					}else if ("湘潭文体广新局".equals(a1.getStationName().trim())) {
						if (field.get(a1) != null) {
							y2Value += field.get(a1) + ",";
						} else {
							y2Value += 0 + ",";
						}
						DateTime dt = new DateTime(a1.getObservTime());
						if("时".equals(radio)){
							xValue1 += "'"+dt.toDateString()+ " " +dt.GetHour() + "',";
						}else{
							xValue1 += "'" +dt.toDateString() + "',";
						}

						//列表数据
						listData += "{\"" + xValue1 +"\":\"" + xValue1
								+ "\"},";
					}
				}
			}
		}
		xValue = xValue + xValue1;
		if (xValue.endsWith(",")) {
			xValue = xValue.substring(0, xValue.length() - 1);
		}
		xValue = xValue + "]";
		if (y1Value.endsWith(",")) {
			y1Value = y1Value.substring(0, y1Value.length() - 1);
		}
		y1Value = y1Value + "]";
		if (y2Value.endsWith(",")) {
			y2Value = y2Value.substring(0, y2Value.length() - 1);
		}
		y2Value = y2Value + "]";
		y1Value = "[{name:'湘潭一中',data:" + y1Value + "},";
		y2Value = "{name:'湘潭文体广新局',data:" + y2Value + "}]";
		String result = y1Value + y2Value;
		return xValue + "|" + result;
	}


	/**
	 * 首页站点实况数据
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/syeverydaysk")
	@ResponseBody
	public Object syeverydaysk(HttpServletRequest request) throws Exception {
		List<airpollution> list = eMServiceImpl.syeverydaysk();
		return list;
	}

}
